package com.jfinal.club._admin.college;

import java.util.List;
import com.jfinal.club.common.model.College;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class CollegeAdminService {

	private College dao = new College().dao();
	
	public Page<College> paginate(int pageNum) {
		return dao.paginate(pageNum, 10, "select *", "from college order by id desc");
	}
	
	public boolean exists(int collegeId, String name) {
		name = name.toLowerCase().trim();
		String sql = "select id from college where coname = ? and id != ? limit 1";
		Integer id = Db.queryInt(sql, name, collegeId);
		return id != null;
	}
	
	public List<College> getAll() {
		List<College> list = dao.find("select * from college order by coname");
		return list;
	}
	
	public Ret save(int id, College college) {
//		area.setId(id);
		college.setConame(college.getConame().trim());
		college.save();
		return Ret.ok("msg", "创建成功");
	}
	
	public College edit(int id) {
		return dao.findById(id);
	}
	
	public Ret update(College college) {
		if (exists(college.getId(), college.getConame())) {
			return Ret.fail("msg", "学院已经存在");
		}

		college.setConame(college.getConame().trim());
		college.update();
		return Ret.ok("msg", "修改成功");
	}
	
	public Ret delete(int collegeId) {
		dao.deleteById(collegeId);
		return Ret.ok("msg", "学院删除成功");
	}
}
